﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BySoft.Server.Business;
using BySoft.Server.Common;
using BySoft.Web.Utility;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using OneSoft.EntityCore;
using BySoft.Web.Controls;

namespace BySoft.Web.Site.SysManage
{
    public partial class ParameterTypeList :  AuthBasePage
    {


        #region 初始化及绑定控件
        /// <summary>
        ///  参数类别查询
        /// </summary>
        public void BindTypeList(int index)
        {
            DataTable dt = BySoft.Server.Business.SelectionCatche.QueryParamerterType(txtTypeID.Text.Trim(), txtTypeName.Text.Trim());
            pcData.RecordCount = dt.Rows.Count;
            pcData.CurrentIndex = index;
            grdResult.DataSource = CommonMethod.GetPagedTable(dt, index, pcData.PageSize);
            if (dt.Rows.Count != 0)
            {
                grdResult.DataBind();
            }
            else
            {
                UIHelper.BindNullSGridView(grdResult);
            }

        }
        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindTypeList(0);
            }
        }

        /// <summary>
        ///  类别查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindTypeList(0);
        }


        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            BindTypeList(e.PageIndex);
            //设置参数类别为当前标签
        }

        /// <summary>
        ///   行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void sgvTypeList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                SysParametertypeEntity entity = SysParametertype.GetSysParametertypeByID(e.CommandArgument.ToString());
                if (entity.IsSolidify == ((int)BaseEnum.IsSolidify.Yes).ToString())
                {
                    UIHelper.AlertMessage(this.Page, Message.SY_DeleteSolidParameterType);
                    return;
                }
                DataTable dt = BySoft.Server.Business.SelectionCatche.GetParametersByType(entity.ParameterTypeID);
                if (dt != null && dt.Rows.Count != 0)
                {
                    UIHelper.AlertMessage(this.Page, "此参数类型不能删除，已经有参数了");
                    return;
                }

                bool bResult = false;
                if (SysParametertype.Delete(e.CommandArgument.ToString()))
                {
                    UIHelper.AlertMessage(this.Page, Message.DeleteSuccess);
                    BindTypeList(0);
                    bResult = true;
                }
                else
                {
                    UIHelper.AlertMessage(this.Page, Message.DeleteFail);
                }

                base.LogAction(BaseEnum.ActionType.Delete, entity.ParameterTypeID, bResult, null, new EntityBase[] { entity });

            }
        }

        /// <summary>
        /// 行数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void sgvTypeList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                #region 删除绑定
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }
                #endregion

                if (e.Row.Cells[2].Text.Length > 30)
                {
                    e.Row.Cells[2].ToolTip = e.Row.Cells[2].Text;
                    e.Row.Cells[2].Text = CommonMethod.GetUnicodeSubString(e.Row.Cells[2].Text, 40, "...");//描述太长，截取显示        
                }
            }

        }
        #endregion

        #region 本地方法

        #endregion

        #region 校验方法
        #endregion
    }
}
